package com.szt.mobile.inventory.modules.login.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.szt.mobile.inventory.R;
import com.szt.mobile.inventory.common.constants.IntfConstants;
import com.szt.mobile.inventory.common.entity.Login;
import com.szt.mobile.inventory.common.ui.BaseActivity;
import com.szt.mobile.inventory.common.utils.DialogUtil;
import com.szt.mobile.inventory.common.utils.GsonUtil;
import com.szt.mobile.inventory.common.utils.SpUtil;
import com.szt.mobile.inventory.common.utils.ToastUtils;
import com.szt.mobile.inventory.common.utils.Xutils;
import com.szt.mobile.inventory.modules.login.biz.LoginBiz;

import org.xutils.common.util.LogUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yanzhaodi on 2017/9/13.
 */
public class ConfigManagerActivity extends BaseActivity {
    private Login mLogin;
    @BindView(R.id.et_config_ip)
    EditText ipEt;

    @BindView(R.id.et_config_port)
    EditText portEt;
    private Xutils mXutils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_config_manager);
        ButterKnife.bind(this);
        String ip = SpUtil.getInstance(this).getKeyStringValue(SpUtil.Key.KEY_IP, "123.206.29.18");
        String port = SpUtil.getInstance(this).getKeyStringValue(SpUtil.Key.KEY_PORT, "8080");
        mXutils = Xutils.getInstance();
        ipEt.setText(ip);
        portEt.setText(port);
    }

    private String ip;
    private String port;

    public void save(View view) {
        ip = ipEt.getText().toString().trim();
        if (TextUtils.isEmpty(ip)) {
            showToast("请输入ip");
            return;
        }
        IntfConstants.IP = ip;

        port = portEt.getText().toString().trim();
        if (TextUtils.isEmpty(port)) {
            showToast("请输入端口");
            return;
        }
        IntfConstants.PORT = port;

        IntfConstants.setBaseUrl();
        SpUtil.getInstance(getApplicationContext()).saveKeyStringValue(SpUtil.Key.KEY_IP, ip);
        SpUtil.getInstance(getApplicationContext()).saveKeyStringValue(SpUtil.Key.KEY_PORT, port);
        ToastUtils.getInstanc(this).showToast("保存成功");


//        testNetwork();
    }

    /**
     * 用登录接口测试网络是否可用
     */
    private void testNetwork() {

        LoginBiz.loginTest(mXutils, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                mLogin = GsonUtil.GsonToBean(result, Login.class);
                if (mLogin.getData().getResultCode().equals("2")) {
                    SpUtil.getInstance(getApplicationContext()).saveKeyStringValue(SpUtil.Key.KEY_IP, ip);
                    SpUtil.getInstance(getApplicationContext()).saveKeyStringValue(SpUtil.Key.KEY_PORT, port);
                    DialogUtil.showSingleBtnDialog(ConfigManagerActivity.this, "IP连接成功", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            showAsynDataDialog();
                        }
                    });
                } else {
                    IntfConstants.IP = "";
                    IntfConstants.PORT = "";
                    IntfConstants.setBaseUrl();
                    DialogUtil.showSingleBtnDialog(ConfigManagerActivity.this, "IP连接失败", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                }
            }

            @Override
            public void onError(String e) {
                LogUtil.e("服务器连接失败");
            }
        });
    }

    private void showAsynDataDialog() {
        DialogUtil.showTwoBtnDialog(ConfigManagerActivity.this, "是否初始化本地数据库？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                File file = new File(getFilesDir().getParent() + "/databases");
                if (file.exists()) {
                    for (File f : file.listFiles()) {
                        f.delete();
                    }
                }
                finish();
            }
        }, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
